import {Select} from 'antd';
import React from 'react'
import {Raw} from 'types';

type SelectProps = React.ComponentProps<typeof Select>

interface IdSelectProps extends Omit<SelectProps, 'value' | 'onChange' | 'defaultOptonName' | 'options'> {
    value: Raw | null | undefined,
    onChange: (value?: number) => void,
    defaultOptonName: string,//默认值
    options?: { name: string, id: number }[] //shuzu1
}

/**
 * value 可以传入多种类型的值
 * onChange只会回调 number|undefined 类型
 * 当 isNaN(Number(value)) 为true的时候,代表选择默认类型
 * 当选择默认类型的时候，onChange会回调undefined
 * @param props
 * @constructor
 */
export const IdSelect = (props: IdSelectProps) => {
    const {value, onChange, defaultOptonName, options, ...restPrps} = props
    return <Select
        value={options?.length ? toNumber(value) : 0}
        onChange={value => onChange(toNumber(value) || undefined)}
        {...restPrps}
    >
        {
            defaultOptonName ? <Select.Option value={0}>{defaultOptonName}</Select.Option> : null
        }
        {
            options?.map(option => <Select.Option key={option.id} value={option.id}>{option.name}</Select.Option>)
        }

    </Select>

}
const toNumber = (value: unknown) => isNaN(Number(value)) ? 0 : Number(value)
